package studiodiva.portfolio.client.view.particleengine;

import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.ImageElement;

import java.util.List;

import static studiodiva.portfolio.client.lib.lang.Lang.list;

public class MouseParticles extends CanvasDisplay implements Object2D {

  private static final double DRAG = 0.7;
  private static final int GRAVITY = -1;
  static final int SCREEN_HEIGHT = 130;
  static final int SCREEN_WIDTH = 200;

  private final ImageElement image = Document.get().createImageElement();
  private final List<Particle> particles = list();
  private final List<Particle> spareParticles = list();

  public MouseParticles() {
    image.setSrc("large_particle_bluelight.png");
    add(this);
  }

  public void draw(Context2d canvas2d) {
    if (spareParticles.size() == 0)
      particles.add(new Particle());
    else spareParticles.remove(0).reset();

    // canvas2d.setFillStyle("rgba(0,0,0,0.3)");
    // canvas2d.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);

    // Collections.sort(particles, new Comparator<Particle>() {
    // public int compare(Particle a, Particle b) {
    // return (int) (b.getPos().getZ() - a.getPos().getZ());
    // }
    // });

    for (Particle particle : particles) {
      particle.update(DRAG, GRAVITY);
      if (particle.isEnabled() && particle.getPos().getY() < -35) {
        particle.disable();
        spareParticles.add(particle);
      }
      if (particle.isEnabled()) particle.draw(canvas2d, image);
    }
  }

  public void update() {}
}
